﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Collections;
using System.Web.UI.HtmlControls;
using System.Text;
using CoolERP_Common;

namespace CoolERP_Web.User
{
    public partial class ZL_WL_Query : BasePage
    {
        static Hashtable htDW = new Hashtable();
        static Hashtable htYS = new Hashtable();
        // 下拉框数组
        private List<DropDownList> listDropDownList;
        // HtmlRow数组
        private static List<TableRowItem> listItems = new List<TableRowItem>();
        private List<Label> listLabel;
        private List<HtmlTableCell> listTableRow;
        protected void Page_Load(object sender, EventArgs e)
        {
            InitAllList();
            if (!IsPostBack)
            {
                BindFirst();
                BindData();
            }
        }

        private void InitAllList()
        {
            listDropDownList = new List<DropDownList>();
            listDropDownList.Add(DropDownList1);
            listDropDownList.Add(DropDownList2);
            listDropDownList.Add(DropDownList3);
            listDropDownList.Add(DropDownList4);
            listDropDownList.Add(DropDownList5);
            listDropDownList.Add(DropDownList6);
            listDropDownList.Add(DropDownList7);
            listDropDownList.Add(DropDownList8);

            listLabel = new List<Label>();
            listLabel.Add(Label1);
            listLabel.Add(Label2);
            listLabel.Add(Label3);
            listLabel.Add(Label4);
            listLabel.Add(Label5);
            listLabel.Add(Label6);
            listLabel.Add(Label7);
            listLabel.Add(Label8);

            listTableRow = new List<HtmlTableCell>();
            listTableRow.Add(td1);
            listTableRow.Add(td2);
            listTableRow.Add(td3);
            listTableRow.Add(td4);
            listTableRow.Add(td5);
            listTableRow.Add(td6);
            listTableRow.Add(td7);
            listTableRow.Add(td8);
        }

        private void BindFirst()
        {
            this.AspNetPager1.PageSize = GridView1.PageSize;
            BindWLLX(this.ddlWLLX, "请选择");
            BindGYS(this.ddlGYSID, "请选择");
            using (CS_JBDWBLL jBLL = new CS_JBDWBLL())
            {
                foreach (CS_JBDW item in jBLL.GetAll())
                    htDW[item.BH] = item.MC;
            }
            using (CS_YSBLL yBLL = new CS_YSBLL())
            {
                foreach (CS_YS item in yBLL.GetAll())
                    htYS[item.YSLXBH + "," + item.BH] = item.MC;
            }
        }

        private void BindData()
        {
            Dictionary<string, string> dc = new Dictionary<string, string>();
            if (!string.IsNullOrEmpty(tbBH.Text))
                dc.Add("BH", tbBH.Text.Trim());
            if (!string.IsNullOrEmpty(tbMC.Text))
                dc.Add("MC", tbMC.Text.Trim());
            if (!string.IsNullOrEmpty(ddlWLLX.SelectedValue))
                dc.Add("WLLX", ddlWLLX.SelectedValue);
            if (!string.IsNullOrEmpty(ddlGYSID.SelectedValue))
                dc.Add("GYS", ddlGYSID.SelectedValue);
            //附加属性查询选择条件的添加
            foreach (TableRowItem item in listItems)
            {
                if (item.Enable)
                {
                    int c = item.Index + 1;
                    switch (c)
                    {
                        case 1:
                            if (!string.IsNullOrEmpty(DropDownList1.SelectedValue)&&DropDownList1.SelectedIndex!=0)
                                dc.Add("SXZBH1", DropDownList1.SelectedValue);
                            break;
                        case 2:
                            if (!string.IsNullOrEmpty(DropDownList2.SelectedValue) && DropDownList2.SelectedIndex != 0)
                                dc.Add("SXZBH2", DropDownList2.SelectedValue);
                            break;
                        case 3:
                            if (!string.IsNullOrEmpty(DropDownList3.SelectedValue) && DropDownList3.SelectedIndex != 0)
                                dc.Add("SXZBH3", DropDownList3.SelectedValue);
                            break;
                        case 4:
                            if (!string.IsNullOrEmpty(DropDownList4.SelectedValue) && DropDownList4.SelectedIndex != 0)
                                dc.Add("SXZBH4", DropDownList4.SelectedValue);
                            break;
                        case 5:
                            if (!string.IsNullOrEmpty(DropDownList5.SelectedValue) && DropDownList5.SelectedIndex != 0)
                                dc.Add("SXZBH5", DropDownList5.SelectedValue);
                            break;
                        case 6:
                            if (!string.IsNullOrEmpty(DropDownList6.SelectedValue) && DropDownList6.SelectedIndex != 0)
                                dc.Add("SXZBH6", DropDownList6.SelectedValue);
                            break;
                        case 7:
                            if (!string.IsNullOrEmpty(DropDownList7.SelectedValue) && DropDownList7.SelectedIndex != 0)
                                dc.Add("SXZBH7", DropDownList7.SelectedValue);
                            break;
                        case 8:
                            if (!string.IsNullOrEmpty(DropDownList8.SelectedValue) && DropDownList8.SelectedIndex != 0)
                                dc.Add("SXZBH8", DropDownList8.SelectedValue);
                            break;
                    }
                }
            }
            using (ZL_WLBLL zBLL = new ZL_WLBLL())
            {
                int RecordCount = 0;
                List<ZL_WL> itemLists = zBLL.GetWL(dc, "BH", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                GridView1.DataSource = itemLists;
                GridView1.DataBind();
                this.AspNetPager1.RecordCount = RecordCount;
                this.AspNetPager1.Visible = RecordCount > 0;
                panMsg.Visible = !this.AspNetPager1.Visible;
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Literal liYS = e.Row.FindControl("liYS") as Literal;
                Literal liDW = e.Row.FindControl("liDW") as Literal;
                Literal liWLLX = e.Row.FindControl("liWLLX") as Literal;
                Literal liGYS = e.Row.FindControl("liGYS") as Literal;
                ZL_WL wl = e.Row.DataItem as ZL_WL;
                liDW.Text = Convert.ToString(htDW[wl.DWBH]);
                liYS.Text = Convert.ToString(htYS[wl.YSLXBH + "," + wl.YSBH]);
                if (ddlWLLX.Items.FindByValue(wl.LXBH) != null)
                    liWLLX.Text = ddlWLLX.Items.FindByValue(wl.LXBH).Text;
                if (ddlGYSID.Items.FindByValue(wl.GYSID.ToString()) != null)
                    liGYS.Text = ddlGYSID.Items.FindByValue(wl.GYSID.ToString()).Text;
            }
        }

        protected void btnQuery_Click(object sender, ImageClickEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            BindData();
        }

        protected void btnDel_Click(object sender, ImageClickEventArgs e)
        {
            StringBuilder sbIds = new StringBuilder();
            foreach (GridViewRow container in GridView1.Rows)
            {
                HtmlInputCheckBox cBox = (HtmlInputCheckBox)container.FindControl("chkSel");
                if (cBox != null && cBox.Checked)
                    sbIds.Append(string.Format(",{0}", cBox.Value));
            }
            if (sbIds.Length > 0)
            {
                sbIds = sbIds.Remove(0, 1);
                using (ZL_WLBLL zBLL = new ZL_WLBLL())
                {
                    if (zBLL.Deletes("ID", sbIds.ToString()))
                        this.ShowMessage("删除成功");
                    else
                        this.ShowMessage("删除失败");
                }
                BindData();
            }
        }

        protected class TableRowItem
        {
            public int Index { get; set; }
            public HtmlTableCell Tr { get; set; }
            public string Key { get; set; }
            public string Value { get; set; }
            public DropDownList Ddl { get; set; }
            public List<CS_WLSXZ> Data { get; set; }
            public string Wllxbh { get; set; }
            public Label Lbl { get; set; }
            public bool Enable { get; set; }

            public TableRowItem(int index, string key, string value, DropDownList ddl, Label label, HtmlTableCell tr, string wllxbh)
            {
                this.Index = index;
                this.Key = key;
                this.Value = value;
                this.Ddl = ddl;
                this.Wllxbh = wllxbh;
                this.Lbl = label;
                this.Tr = tr;
                Init();
            }

            private void Init()
            {
                this.Lbl.Text = Value + "：";
                // 获取属性值
                using (CS_WLSXZBLL wBLL = new CS_WLSXZBLL())
                {
                    List<CS_WLSXZ> list = wBLL.GetList(t => t.WLLXBH == this.Wllxbh && t.WLSXBH == this.Key);
                    this.Ddl.Items.Clear();
                    if (list.Count > 0)
                    {
                        this.Ddl.DataSource = list;
                        this.Ddl.DataTextField = "MC";
                        this.Ddl.DataValueField = "BH";
                        this.Ddl.DataBind();
                        this.Enable = true;
                    }
                    else
                    {
                        this.Enable = false;
                    }
                }
                this.Ddl.Items.Insert(0, new ListItem("请选择", "0"));
            }
        }

        protected void ddlWLLX_SelectedIndexChanged(object sender, EventArgs e)
        {
            td1.Visible = false;
            td2.Visible = false;
            td3.Visible = false;
            td4.Visible = false;
            td5.Visible = false;
            td6.Visible = false;
            td7.Visible = false;
            td8.Visible = false;
            // 根据类型获取属性
            ShowSXDropDownList(ddlWLLX.SelectedValue.Trim());
            //liLXBH.Text = ddlLXBH.SelectedValue;
        }

        private void ShowSXDropDownList(string type)
        {
            using (CS_WLLXSXBLL wBLL = new CS_WLLXSXBLL())
            {
                List<CS_WLLXSX> listWLLXSX = wBLL.GetList(t => t.WLLXBH == type);
                listItems.Clear();
                if (listWLLXSX.Count > 0)
                {
                    // 将8个属性下拉框依次初始化
                    for (int i = 0; i < listWLLXSX.Count; i++)
                    {
                        string key = listWLLXSX[i].BH;
                        string value = listWLLXSX[i].MC;
                        DropDownList ddl = listDropDownList[i];
                        Label lbl = listLabel[i];
                        HtmlTableCell tr = listTableRow[i];
                        TableRowItem tri = new TableRowItem(i, key, value, ddl, lbl, tr, this.ddlWLLX.SelectedValue);
                        listItems.Add(tri);
                    }
                    tr1.Visible = true;
                    if (listWLLXSX.Count > 4)
                        tr2.Visible = true;
                    // 显示row
                    foreach (TableRowItem item in listItems)
                        item.Tr.Visible = item.Enable;
                }
                else
                {
                    td1.Visible = false;
                    td2.Visible = false;
                    td3.Visible = false;
                    td4.Visible = false;
                    td5.Visible = false;
                    td6.Visible = false;
                    td7.Visible = false;
                    td8.Visible = false;
                }
            }
        }
    }
}